/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet;

import Model.BeanUltility.ResourceMapper;
import Model.ObjectBean.Resource;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tran Nguyen Anh Tu
 */
public class ResourceDetailServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        
        String isInStock="";
        Resource item=ResourceMapper.getResource(req.getParameter("resourceID"));
        req.setAttribute("itemdetail_reid", item.getRe_id());
        req.setAttribute("itemdetail_group", item.getGroup_id());
        req.setAttribute("itemdetail_label", item.getLabel());
        req.setAttribute("itemdetail_category", item.getCategory_id());
        req.setAttribute("itemdetail_author", item.getAuthor());
        req.setAttribute("itemdetail_pagenumber", item.getPage_num());
        req.setAttribute("itemdetail_publisher", item.getPublisher_id());
        req.setAttribute("itemdetail_publishdate", dateConverter(item.getPublish_date()));
        req.setAttribute("itemdetail_translator", item.getTranslator());
        req.setAttribute("itemdetail_price", addSpace(calculateDiscount(item.getPrice(),item.getDiscount())));
        if(Integer.parseInt(item.getStock())==0) isInStock="Hết hàng";
        else isInStock="Còn hàng";
        req.setAttribute("itemdetail_instock", isInStock);
        req.setAttribute("itemdetail_introduce", item.getIntroduce());
        req.setAttribute("itemdetail_coverimg", item.getCover_image());
               
        RequestDispatcher dispatcher = req.getRequestDispatcher("MainPages/itemdetail.jsp");
        dispatcher.forward(req, resp);
    }

    //ADD SPACE INTO PRICE (IMPROVE UI)
    //Ex: xxxxxxx -> x xxx xxx
    //Algorithm: 
    //  S1: xxxxxxx
    //  S2: xxxxyyy temp[0]=yyy;
    //  S3: xcccxxx temp[1]=ccc;
    //  S4: zxxxxxx temp[2]=z;
    // result = temp[2]+","+temp[1]+","+temp[0];
    public String addSpace(String price){
        String temp[]=new String[6];
        String result="";
        int i;
        for(i =0;i<price.length()/3;i++)
        {
        temp[i]=price.substring(price.length()-3*(i+1),price.length()-3*i);
        }
        temp[i]=price.substring(0,price.length()-3*i);
        while(i>=0)
        {
            result+=temp[i]+" ";
            i--;
        }
        return result;
    }
    
    //DATE CONVERTER (IMPROVE UI)
    //Convert from yyyy/mm/dd to dd/mm/yyyy
    public String dateConverter(String date){
        String[] temp=date.split("-",3);
        return temp[2]+"/"+temp[1]+"/"+temp[0];
    }
    
    //CALCULATE DISCOUNTED PRICE
    public String calculateDiscount(String price, String discount){
        int Price = Integer.parseInt(price);
        DecimalFormat df = new DecimalFormat("#"); 
        double Discount=Double.parseDouble(discount);
        return df.format(Price*(1-Discount));
    }
}
